package com.rzn.reading.controller;


import com.rzn.reading.BusinessException;
import com.rzn.reading.JsonResult;
import com.rzn.reading.Message;
import com.rzn.reading.model.dto.user.UpdatePasswordDTO;
import com.rzn.reading.model.dto.user.UserCreateInputDTO;
import com.rzn.reading.model.dto.user.UserInfoDTO;
import com.rzn.reading.model.dto.user.UserUpdateDTO;
import com.rzn.reading.model.param.LoginParam;
import com.rzn.reading.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author rzn
 * @since 2022-02-12
 */
@RestController
@RequestMapping("//user")
@Api(tags = "用户")
public class UserController {

    @Autowired
    private UserService userService;

    @ApiOperation(value = "登录")
    @PostMapping("/login")
    public JsonResult login(@RequestBody LoginParam loginParam) {
        UserInfoDTO userInfoDTO = userService.login(loginParam);
        return JsonResult.success(userInfoDTO);
    }


    @PostMapping("/register")
    @ApiOperation("注册")
    public JsonResult register(@RequestBody UserCreateInputDTO userCreateInputDTO){
        Boolean res = userService.register(userCreateInputDTO);
        if (!res) {
            throw new BusinessException(new Message("DB.ADD_FAILED", "用户").toString());
        }
        return JsonResult.success(new Message("DB.ADD_SUCCESS", "用户").toString());
    }

    @PostMapping("/update")
    @ApiOperation("修改个人信息")
    public JsonResult updateUserInfo(@RequestBody UserUpdateDTO userUpdateDTO){
        Boolean res = userService.updateUserInfo(userUpdateDTO);
        if (!res) {
            throw new BusinessException(new Message("DB.UPDATE_FAILED", "用户").toString());
        }
        return JsonResult.success(new Message("DB.UPDATE_SUCCESS", "用户").toString());
    }

    @PostMapping("/updatePassword")
    @ApiOperation("修改密码")
    public JsonResult updatePassWord(@RequestBody UpdatePasswordDTO userUpdateDTO){
        Boolean res = userService.updatePassword(userUpdateDTO);
        if (!res) {
            throw new BusinessException(new Message("DB.UPDATE_FAILED", "密码").toString());
        }
        return JsonResult.success(new Message("DB.UPDATE_SUCCESS", "密码").toString());
    }
}

